package cn.catsdad.tenant.sqlInspector;

import cn.catsdad.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 租户实体注册表
 *
 * @author xuyl
 * @version 1.0 2024/8/3
 */
@Component
public class TenantEntityRegistry {

    private final Map<String, EntityDefinition> entityDefinitionMap = new ConcurrentHashMap<>();

    public void registerEntity(String entityName, EntityDefinition definition) {
        this.entityDefinitionMap.put(entityName, definition);
    }

    public EntityDefinition getEntityDefinition(String entityName) {
        return this.entityDefinitionMap.get(entityName);
    }

    /**
     * 判断是否是租户表
     *
     * @param tableName 表明
     * @return 判断结果
     */
    public boolean isTenantTable(String tableName) {
        boolean result = false;
        if (StringUtil.isNotEmpty(tableName)) {
            Collection<EntityDefinition> values = entityDefinitionMap.values();
            result = values.stream().anyMatch(e -> e.getTableName().equals(tableName));
        }
        return result;
    }
}
